package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService articleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiverMessage(ConsumerRecord<?,?>record){
        log.info("接收id开始更新状态:{}",record.value());

        if (Optional.ofNullable(record).isPresent()){
            Map map = JSON.parseObject((String) record.value(), Map.class);
            int enable =(Integer) map.get("enable");
            System.out.println(enable+"**********************enable: "+(enable^1));
            articleConfigService.update(
                    Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId,map.get("articleId"))
                    .set(ApArticleConfig::getIsDown,(enable^1))
            );
        }
        log.info("*******************更新结束*************************");
    }
}
